#!/usr/bin/env bash
#######################################
have_you_encountered_signal9() {
    if ("${TUI_BIN:-whiptail}" --title "Android 12(signal9)" --yesno "Have you encountered this problem:\n [Process completed (signal 9) - press Enter]?" 0 50); then
        check_adb_command
        connect_to_adb_device
    else
        tmoe_manager_main_menu
    fi
}
check_adb_command() {
    if [[ -z "$(command -v adb)" ]]; then
        set_adb_pkg_name
        install_android_tools
    fi
}
set_adb_pkg_name() {
    case "$LINUX_DISTRO" in
    debian) DEPENDENCY_01="adb" ;;
    termux | *) DEPENDENCY_01="android-tools" ;;
    esac
}
install_android_tools() {
    unset DEPENDENCY_02
    printf "%s\n" "${GREEN}${TMOE_INSTALLATION_COMMAND} ${BLUE}${DEPENDENCY_01}${RESET}"
    do_you_want_to_continue
    ${TMOE_UPDATE_COMMAND} 2>/dev/null
    ${TMOE_INSTALLATION_COMMAND} ${DEPENDENCY_01}
}
type_adb_address() {
    case ${TMOE_LANG} in
    zh_*UTF-8) local _Description="请打开android 设备的 adb 无线调试功能，\n并在下方输入 adb 地址。\n若留空则使用 127.0.0.1:5555" ;;
    *) local _Description="Please enable the adb wireless debugging\n on your android device\n and type the adb address here.\nIf left blank use 127.0.0.1:5555" ;;
    esac

    ADB_ADDRESS=$("${TUI_BIN:-whiptail}" --inputbox "$_Description" 0 50 --title "ADB address" 3>&1 1>&2 2>&3)
    if [ "$?" != "0" ]; then
        printf "%s\n" "${YELLOW}Unknown ${RED}ERROR${RESET}"
        press_enter_to_return
        have_you_encountered_signal9
    elif [ -z "${ADB_ADDRESS}" ]; then
        ADB_ADDRESS="127.0.0.1:5555"
    fi
}
do_you_want_to_set_max_phantom_processes_to_a_big_num() {
    printf "%s\n" \
        "Do you want to set ${YELLOW}the maximum number of phantom processes to ${BLUE}2147483647${RESET}?" \
        "${GREEN}device_config ${YELLOW}put activity_manager max_phantom_processes ${BLUE}2147483647${RESET}"
    RETURN_TO_WHERE="do_you_want_to_disable_monitor_phantom_procs"
    do_you_want_to_skip || return 1
}
get_max_phantom_processes_0() {
    adb shell "/system/bin/dumpsys activity settings | grep max_phantom_processes"
}
get_max_phantom_processes_1() {
    adb -s "$ADB_ADDRESS" shell "/system/bin/dumpsys activity settings | grep max_phantom_processes"
}
do_you_want_to_disable_monitor_phantom_procs() {
    printf "%s\n" \
        "Do you want to ${RED}disable ${YELLOW}monitor_phantom_procs${RESET}?" \
        "${GREEN}settings ${YELLOW}put global settings_enable_monitor_phantom_procs ${BLUE}false${RESET}"
    RETURN_TO_WHERE="do_you_want_to_set_sync_disabled"
    do_you_want_to_skip || return 1
}
disable_monitor_phantom_procs_0() {
    adb shell "/system/bin/settings put global settings_enable_monitor_phantom_procs false"
}
disable_monitor_phantom_procs_1() {
    adb -s "$ADB_ADDRESS" shell "/system/bin/settings put global settings_enable_monitor_phantom_procs false"
}
do_you_want_to_set_sync_disabled() {
    # device_config set_sync_disabled_for_tests persistent
    printf "%s\n" \
        "Do you want to ${RED}set sync ${YELLOW}disabled${RESET}?" \
        "${GREEN}device_config ${YELLOW}set_sync_disabled_for_tests ${BLUE}persistent${RESET}"
    RETURN_TO_WHERE="if_you_are_unable_to_resolve_android_12_signal9"
    do_you_want_to_skip || return 1
}
set_sync_disabled_0() {
    adb shell "/system/bin/device_config set_sync_disabled_for_tests persistent"
}
set_sync_disabled_1() {
    adb -s "$ADB_ADDRESS" shell "/system/bin/device_config set_sync_disabled_for_tests persistent"
}
are_you_using_samsung_device() {
    case ${TMOE_MENU_LANG} in
    zh_*UTF-8)
        local _Yes="否"
        local _No="是"
        local _Description="请问您是否在使用三星的android设备?\n注:三星的系统,在开启adb调试时,可能会出现\"could not install *smartsocket* listener\"的问题。\n当您选择\"是\"时，将会使用兼容模式来启动 adb-server。\n开启该模式后，您可能需要使用局域网地址(例如:192.168.x.y:zz)来连接adb, 而不是本机地址(127.0.0.1:xxyy)"
        ;;
    *)
        local _Yes="No"
        local _No="Yes"
        local _Description="Are you using a \"samsung\" android device?\nOr, have you encountered this problem:\n\"could not install *smartsocket* listener\"?\nNote:If so, then compatibility mode will be switched on. Once enabled, you may need to use a LAN address (e.g. 192.168.x.y:zz) to connect to adb, rather than the localhost(e.g. 127.0.0.1:xxyy)"
        ;;
    esac

    if ("${TUI_BIN:-whiptail}" --title "SAMSUNG" --yes-button "$_Yes" --no-button "$_No" --yesno "$_Description" 0 50); then
        unset TMOE_ADB_PREFIX
        do_you_want_to_restart_adb_server && do_you_want_to_change_the_adb_server_tcp_port
    else
        set_samsung_adb_comp_mode
    fi
}
set_samsung_adb_comp_mode() {
    local _Dep="fakeroot"
    if [[ -z "$(command -v $_Dep)" ]]; then
        DEPENDENCY_01="$_Dep"
        install_android_tools
    fi
    [[ -e ${TMPDIR}/adb.sock ]] && rm -fv ${TMPDIR}/adb.sock
    export ADB_SERVER_SOCKET=localfilesystem:${TMPDIR}/adb.sock
    export ANDROID_NO_USE_FWMARK_CLIENT=1
    TMOE_ADB_PREFIX="fakeroot"
    printf "%s\n" "${PURPLE}pkill ${GREEN}adb${RESET}"
    pkill adb
    printf "%s\n" "${GREEN}${TMOE_ADB_PREFIX} ${GREEN}adb ${PURPLE}kill-server${RESET}"
    ${TMOE_ADB_PREFIX} adb kill-server || adb kill-server
}
adb_pair_or_connect() {
    case ${TMOE_MENU_LANG} in
    zh_*UTF-8)
        local _Yes="配对+连接"
        local _No="连接"
        local _Description="在一般情况下，配对和普通连接的地址是不一样的。第一次连接时，您可以先配对(pair)，再用普通连接。"
        ;;
    *)
        local _Yes="pair+connect"
        local _No="connect"
        local _Description="In general, the addresses for pairing and normal connection are different."
        ;;
    esac

    if ("${TUI_BIN:-whiptail}" --title "Pair/connect" --yes-button "$_Yes" --no-button "$_No" --yesno "If you are connecting for the first time and it provides a pairing code, then use \"pair\"\n${_Description}" 0 50); then
        type_adb_address
        adb_pair_and_connect
    else
        type_adb_address
        adb_connect_address
    fi
}
adb_pair_and_connect() {
    printf "%s\n" "${GREEN}adb ${YELLOW}pair ${BLUE}${ADB_ADDRESS}${RESET}"
    ${TMOE_ADB_PREFIX} adb pair "$ADB_ADDRESS"

    case ${TMOE_MENU_LANG} in
    zh_*UTF-8)
        local _Description_2="请问您的\"普通连接\"的 adb 地址跟 \"配对连接\"的是一样的吗？也是\"${ADB_ADDRESS}\"么?"
        local _Yes="不是"
        local _No="是哒"
        ;;
    *)
        local _Description_2="Is the address of your normal connection also \"${ADB_ADDRESS}\"?"
        local _Yes="No"
        local _No="Yes"
        ;;
    esac

    if ("${TUI_BIN:-whiptail}" --title "pairing address = normal address?" --yes-button "$_Yes" --no-button "$_No" --yesno "${_Description_2}" 0 50); then
        pleae_type_your_normal_adb_address
        adb_connect_address
    else
        adb_connect_address
    fi
}

pleae_type_your_normal_adb_address() {
    local _Description="Please type the normal adb address"

    NORMAL_ADB_ADDRESS=$("${TUI_BIN:-whiptail}" --inputbox "$_Description" 0 50 --title "NORMAL ADB address" 3>&1 1>&2 2>&3)
    if [ -n "${NORMAL_ADB_ADDRESS}" ]; then
        ADB_ADDRESS=${NORMAL_ADB_ADDRESS}
    fi
}
adb_connect_address() {
    printf "%s\n" "${GREEN}adb ${YELLOW}connect ${BLUE}${ADB_ADDRESS}${RESET}"
    ${TMOE_ADB_PREFIX} adb connect "$ADB_ADDRESS" || adb connect "$ADB_ADDRESS"
    ${TMOE_ADB_PREFIX} adb devices -l || adb devices -l
}
do_you_want_to_restart_adb_server() {
    if ("${TUI_BIN:-whiptail}" --title "adb kill-server" --yesno "Do you want to restart the adb server?" 0 50); then
        printf "%s\n" "${PURPLE}pkill ${GREEN}adb${RESET}"
        pkill adb
        printf "%s\n" "${GREEN}adb ${PURPLE}kill-server${RESET}"
        adb kill-server
        return 0
    else
        return 1
    fi
}
do_you_want_to_change_the_adb_server_tcp_port() {
    if ("${TUI_BIN:-whiptail}" --title "adb server tcp port" --yesno "Do you want to set the adb server port to \"50377\"?\nNote: The adb server port is not the adb device port.\nThe default adb server port is 5037.\nBut in tmoe, it is 50377." 0 50); then
        ANDROID_ADB_SERVER_PORT=50377
    else
        ANDROID_ADB_SERVER_PORT=$("${TUI_BIN:-whiptail}" --inputbox "Please type a tcp port(1024~65535)" 0 50 --title "ADB SERVER PORT" 3>&1 1>&2 2>&3)
        if [ "$?" != "0" ]; then
            printf "%s\n" "${YELLOW}Unknown ${RED}ERROR${RESET}"
            ANDROID_ADB_SERVER_PORT=50378
        elif [ -z "${ADB_ADDRESS}" ]; then
            ANDROID_ADB_SERVER_PORT=50379
        fi
    fi
    printf "%s\n" "Your ANDROID_ADB_SERVER_PORT is ${ANDROID_ADB_SERVER_PORT}"
    export ANDROID_ADB_SERVER_PORT
}
set_max_phantom_processes_0() {
    adb shell "/system/bin/device_config put activity_manager max_phantom_processes 2147483647"
}
set_max_phantom_processes_1() {
    adb -s "$ADB_ADDRESS" shell "/system/bin/device_config put activity_manager max_phantom_processes 2147483647"
}
connect_to_adb_device() {
    adb --version
    are_you_using_samsung_device
    # ANDROID_ADB_SERVER_PORT
    adb_pair_or_connect
    ADB_LIST="$(adb devices | wc -l)"
    case ${ADB_LIST} in
    0 | 1 | 2)
        printf "%s\n" "${YELLOW}Unknown ${RED}ERROR${RESET}, you cannot connect to the adb device"
        printf "%s\n" "Please exit tmoe manager, and run ${GREEN}adb ${YELLOW}devices ${BLUE}-l${RESET} to check your adb device"
        press_enter_to_return
        have_you_encountered_signal9
        ;;
    3)
        get_max_phantom_processes_0
        do_you_want_to_set_max_phantom_processes_to_a_big_num && set_max_phantom_processes_0
        get_max_phantom_processes_0
        do_you_want_to_disable_monitor_phantom_procs && disable_monitor_phantom_procs_0
        # device_config set_sync_disabled_for_tests persistent
        do_you_want_to_set_sync_disabled && set_sync_disabled_0
        ;;
    *)
        get_max_phantom_processes_1
        do_you_want_to_set_max_phantom_processes_to_a_big_num && set_max_phantom_processes_1
        get_max_phantom_processes_1
        do_you_want_to_disable_monitor_phantom_procs && disable_monitor_phantom_procs_1
        do_you_want_to_set_sync_disabled && set_sync_disabled_1
        ;;
    esac
    if_you_are_unable_to_resolve_android_12_signal9
}
if_you_are_unable_to_resolve_android_12_signal9() {
    printf "%s\n" "If you are unable to resolve this problem, please try again, and report an issue."
    press_enter_to_return
    tmoe_manager_main_menu
}
################################
have_you_encountered_signal9
